<template>
	<view>
		<view class="equipment-head">
			<image class="devicePic" :src="device.devicePic" mode=""></image>
			<view class="">
				{{$t('connectNet.name_device')}}
			</view>
		</view>
		<view class="equipment-name">
			<uni-easyinput class="uni-mt-5" trim="all" v-model="deviceName" :placeholder="$t('connectNet.enter_device_name')">
			</uni-easyinput>
		</view>
		<view class="foot-button">
			<button class="block radius-btn" @tap="nextstep()" type="operation">{{$t('General.login.nextStep')}}</button>
		</view>
	</view>
</template>

<script>

	const {hexToString,getType,isEmojiCharacter} = require('@/utils/util.js');
	export default {
		data() {
			return {
				connecttype:'',//配网类型
				device: {},
				deviceNo: '', //设备序列号
				deviceType: '', //设备类型
				deviceName: ''
			}
		},
		onLoad(opt) {
			uni.setNavigationBarTitle({
				title: this.$t('connectNet.Modify_device_name')
			});
			this.connecttype = opt.connecttype
			console.log(opt,'设备信息');
			// wx.hideHomeButton();
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			// this.device = JSON.parse(opt.device)
			this.deviceNo = this.device.deviceNo
			this.deviceType = parseInt(this.device.deviceType)
			getApp().globalData.api.getDeviceInfoByPID({
				pid:this.device.pid|| this.deviceType
			}, (result) => {
				console.log(result);
				this.deviceName = result.deviceTypeName + this.deviceNo.substr(-4)
                this.device.devicePic = result.devicePic
			});
			// this.deviceName = this.deviceType + this.deviceNo.substr(-4)
		},
		methods: {
			nextstep() {
				if (!this.deviceName) {
					return uni.showToast({
						title: this.$t('connectNet.enter_device_name'),
						icon: 'none'
					})
				}
				if(isEmojiCharacter(this.deviceName)){
					uni.showToast({
						title: this.$t('connectNet.device_name_not_emoji'),
						icon:'none'
					});
					return;
				}
				this.device.deviceName = this.deviceName
				this.addDevice()
			},
			//添加设备
			addDevice() {
				uni.showLoading({
					title: this.$t('connectNet.adding_device')
				})
				const data = {
					userGroupId: uni.getStorageSync('currentGroupId'),
					deviceNo: this.device.deviceNo,
					deviceName: this.device.deviceName,
					devUserName:'admin',
					pid: this.device.pid || this.deviceType,
					nickName: uni.getStorageSync('userInfo').nickName,
					timeZone: 'Asia/Shanghai',
					addType:'net',
					adminToken:this.device.adminToken||'',
					devPassWord: this.device.devPassWord ? this.device.devPassWord : '',
					// devUserName:'admin'
				};
				getApp().globalData.api.addDevice(data, (result) => {
					console.log(result,'添加设备接口返回')
					uni.hideLoading()
					if(result.data.code == 2000){
						switch (this.connecttype){
							case 'qrcode':
								this.$postPointData({code:'601400', eventName:'qr_sys_add_dev_success'})
								break;
							case 'wifi':
								this.$postPointData({code:'601010', eventName:'wifi_sys_add_dev_success'})
								break;
							case 'bluetooth':
								this.$postPointData({code:'601100', eventName:'blue_sys_add_dev_success'})
								let sns = uni.getStorageSync('interSns')?uni.getStorageSync('interSns'):[];
								if (!sns.includes(this.device.deviceNo)) {
								  sns.push(this.device.deviceNo);
								}
								uni.setStorageSync('interEnable',true)
								uni.setStorageSync('interSns',sns)
								break;
						}
						uni.showToast({
							title: this.$t('connectNet.device_added_successfully'),
							icon: 'none',
							duration: 2000,
							success: () => {
								setTimeout(() => {
									uni.navigateTo({
										url: '/jvss/pagesDevice/device/room-select/room-select?type=1&device=' + encodeURIComponent(JSON.stringify(this.device))+'&connecttype='+this.connecttype
									})
								}, 1000)
							}
						})
					}else{
						switch (this.connecttype){
							case 'qrcode':
								this.$postPointData({code:'401206', eventName:'qr_sys_add_dev_failed',reason:result.data.msg,error_code:result.data.code})
								break;
							case 'wifi':
								this.$postPointData({code:'401004', eventName:'wifi_sys_add_dev_error'})
								break;
							case 'bluetooth':
								this.$postPointData({code:'401106', eventName:'bluetooth_sys_add_dev_failed',reason:result.data.msg,error_code:result.data.code,devId:this.device.deviceNo})
								break;
						}
					}
				},false,false);
			}
		}
	}
</script>

<style>
	.equipment-head {
		text-align: center;
		color: #666;
		margin: 100rpx 0;
		font-size: 28rpx;
	}

	.devicePic {
		width: 100rpx;
		height: 100rpx;
	}

	.equipment-name {
		width: 96%;
		margin: auto;

	}

	.uni-easyinput .is-input-border {
		border: none;
	}

	.uni-easyinput__placeholder-class {
		text-align: center;
	}

	.equipment-head img {
		width: 100rpx;
		height: 100rpx;
		margin-bottom: 40rpx;
	}
</style>
